class UserController < ApplicationController
  before_filter :protect
  def index
    @title = "个人信息"
    @user = @login_user
    if @login_user.is_vip == YES
      @profile = @login_user.profile
    end
    person
  end
  
  def change_password
    @title = "修改密码"
    modify_password
  end
  
  def fav_requirements
    @title = "我收藏的需求"
    @requirements = @login_user.fav_requirements.paginate(
                                :page => params[:page],
                                :per_page => PERPAGE,
                                :order => "created_at DESC")
  end
  
  def fav_comments
    @title = "我收藏的稿件"
    @comments = @login_user.fav_comments.paginate(
                                :page => params[:page],
                                :per_page => PERPAGE,
                                :order => "created_at DESC")
  end
  
  def fav_works
    @title = "我收藏的作品"
    @works = @login_user.fav_works.paginate(
                                :page => params[:page],
                                :per_page => PERPAGE,
                                :order => "created_at DESC")
  end
  
  def orders
    @title = "我的接单"
    @orders = @login_user.orders.paginate(
                                :page => params[:page],
                                :per_page => PERPAGE,
                                :order => "created_at DESC")
  end

  def solved_requirements
    @title = "我解决的需求"
    @requirements = @login_user.solved_requirements.paginate(
                                :page => params[:page],
                                :per_page => PERPAGE,
                                :order => "created_at DESC")
  end
  
  def something_wrong
    @title = "错误的请求"
  end
  
  def delete_fav_requirements
    FavoriteRequirement.delete_favorite(@login_user,params[:delete_ids])
    redirect_to :action => :fav_requirements
  end
  
  def delete_fav_comments
    FavoriteComment.delete_favorite(@login_user,params[:delete_ids])
    redirect_to :action => :fav_comments
  end
  
  def delete_fav_works
    FavoriteWork.delete_favorite(@login_user,params[:delete_ids])
    redirect_to :action => :fav_works
  end
  
  private
  def person
    if param_posted?(:user)
      if @login_user.update_attributes(params[:user])
        flash[:notice] = "修改成功"
      else
        flash[:notice] = "修改失败"
      end
    end
    if param_posted?(:profile)
      @scripts = ["fendu_vip.js"]
#      @profile = @login_user.profile
      if @login_user.profile.update_attributes(params[:profile])
        flash[:notice] = "修改成功"
      else
        flash[:notice] = "修改失败"
      end
    end
  end
  
  def modify_password
    if param_posted?(:user)
      if @login_user.correct_password?(params)
        @login_user.update_password(params)
        User.logout!(session,cookies)
        redirect_to :controller => "admin/users", :action => :login
        return 
      end
    end
    @login_user.clear_password!
  end

end
